package top.laoshuzi.server.userservice.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.thrift.TException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.laoshuzi.server.common.pojo.ResponseResult;
import top.laoshuzi.server.userservice.bo.UserInfoBO;
import top.laoshuzi.server.userservice.dto.UserInfoDTO;
import top.laoshuzi.server.userservice.provider.ThriftServiceProvider;
import top.laoshuzi.server.userservice.utils.constants.ServerErrorConstants;

@Api(tags = {"用户相关"})
@RestController
@RequestMapping(value = "/api/v1/user")
public class UserController {

    @Autowired
    private ThriftServiceProvider thriftServiceProvider;

    @ApiOperation(value = "更新用户信息")
    @ApiResponses(value = {@ApiResponse(code = 200, response = UserInfoDTO.class, message = "Successed")})
    @PatchMapping(value = "/user/info/{userId}")
    public ResponseResult updateUserInfo(@PathVariable("userId") Long id, @RequestBody UserInfoDTO userInfoDTO) {

        UserInfoBO userInfoBO = new UserInfoBO();
        BeanUtils.copyProperties(userInfoDTO, userInfoBO);

        UserInfoBO bo = null;
        try {
            bo = thriftServiceProvider.getUserService().updateUserInfo(id, userInfoBO);
        } catch (TException e) {
            e.printStackTrace();
            return ResponseResult.failed(ServerErrorConstants.THRIFT_SERVER_ERROR.getError());
        }

        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(bo, dto);

        return ResponseResult.successed(dto);
    }


}
